<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .mytime {
            line-height: 40px;
            width: 300px;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="mytime jsTime2" data-time="2020-11-28 16:30:05">时间1</div>
    <div class="mytime jsTime2" data-time="2020-11-28 14:01:22">时间2</div>
    <div class="mytime jsTime2" data-time="2020-11-28 18:06:25">时间3</div>
    <div class="mytime jsTime2" data-time="2020-11-28 22:07:19">时间4</div>
    <div class="mytime jsTime2" data-time="2020-11-28 23:12:38">时间5</div>

    <script>
         //  补0 格式化时间 加入传进来的数字只有一位数就补0 两位数就原地返回啥也不做
        const formatNumber = n => {
            n = n.toString();
            return n[1] ? n : '0' + n;
        };
        //团购倒计时
        const teamCountTime = (obj) => {
            var timer = null;
            function fn() {
                //获取设置的时间 如：2019-3-28 14:00:00
                var setTime = obj.getAttribute('data-time');
                //获取当前时间
                var date = new Date(),
                    now = date.getTime(),
                    endDate = new Date(setTime),
                    end = endDate.getTime();
                //时间差
                var leftTime = end - now;
                //d,h,m,s 天时分秒
                var d, h, m, s;
                var otime = '';
                if (leftTime >= 0) {
                    d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
                    h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
                    m = Math.floor(leftTime / 1000 / 60 % 60);
                    s = Math.floor(leftTime / 1000 % 60);
                    if (d <= 0) {
                        otime = [h, m, s].map(formatNumber).join(':');
                    } else {
                        otime = d + '天' + [h, m, s].map(formatNumber).join(':');
                    }
                    obj.innerHTML = '剩余' + otime;
                    //
                    timer = setTimeout(fn, 1e3);
                } else {
                    clearTimeout(timer);
                    obj.innerHTML = '秒杀已结束';
                }
            }
            fn();
        };
        let jsTimes = document.querySelectorAll('.jsTime2');
        jsTimes.forEach((obj) => {
            teamCountTime(obj);
        });
    </script>

</body>

</html>